# _*_ coding: utf-8 _*_
# @Time : 2024-01-16 21:45
# @Author : wf
import urllib.request
import urllib.parse

base_url = 'https://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=cname'


def create_request(page):
    data = {
        'cname': '北京',
        'pid': '',
        'pageIndex': page,
        'pageSize': '10',
    }
    # 必须编码 + encode
    data = urllib.parse.urlencode(data).encode('utf-8')
    return urllib.request.Request(url=base_url, data=data, )


def get_content(req):
    response = urllib.request.urlopen(req)
    return response.read().decode('utf-8')


def download(content, page):
    with open('kendeji' + str(page) + '.json', 'w', encoding='utf-8') as fp:
        fp.write(content)


if __name__ == '__main__':
    start_page = int(input("起始页数："))
    end_page = int(input("结束页数："))
    for page in range(start_page, end_page + 1):
        req = create_request(page)

        content = get_content(req)

        # 下载文件
        download(content, page)
